const util = {}
const weekObj = [
  '日',
  '一',
  '二',
  '三',
  '四',
  '五',
  '六'
]
util.random_string = function (len) {
  len = len || 32
  const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  const maxPos = chars.length
  let pwd = ''
  for (let i = 0; i < len; i++) {
    pwd += chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return pwd
}

util.getFileType = function (name) {
  return name.substring(name.lastIndexOf('.') + 1)
}

util.findCodeByUrl = function (url) {
  if (url && util.isSelfUrl(url) && url.indexOf('/goods/') > -1) {
    return url.substring(url.lastIndexOf('/')+1)
  }
  return null;
}
util.findContentCodeByUrl = function (url) {
  if (url && util.isSelfUrl(url) && url.indexOf('/contentDetail/') > -1) {
    return url.substring(url.lastIndexOf('/')+1)
  }
  return null;
}
util.isSelfUrl = function (url) {
  if(!/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/.test(url)){
    return false
  }
  // 运行环境或者生产或者测试
  if(url.indexOf('www.xhadujia.com') > -1 || url.indexOf('www.qingdaodujia.cn') > -1 || url.indexOf('wdttest.banajava.com') > -1 || url.indexOf('wdtmtest.banajava.com') > -1){
      return true
  }
  return false
}
util.bMap2TxMap = function (lng, lat){
  const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  const x = lng - 0.0065;
  const y = lat - 0.006;
  const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  const lngs = z * Math.cos(theta);
  const lats = z * Math.sin(theta);
  return {lng: lngs,lat: lats};
}
util.txMap2BMap = function (lng,lat){
  const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  const x = lng;
  const y = lat;
  const z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  const theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  const lngs = z * Math.cos(theta) + 0.0065;
  const lats = z * Math.sin(theta) + 0.006;
  return {lng: lngs,lat: lats};
}
util.url2https = function (url) {
  if (url.indexOf('http') === -1) { // 如果没有http开头，添加http开头
    if (url.startsWith('//')) {
      url = 'https:' + url
    } else {
      url = 'https://' + url
    }
  } else if (url.indexOf('http:') !== -1) { // 如果是http开头，修改成https开头
    url = url.replace('http:', 'https:')
  }
  url = url.replace('wdt-xilvxin.oss-cn-qingdao.aliyuncs.com', 'img.qingdaodujia.cn')
  url = encodeURI(url)
  return url;	
}
util.getQueryString = function (name, url) {
  let paramStr = null;
  if (url && url.lastIndexOf('?') != -1) {
    const inx = url.lastIndexOf('?');
    paramStr = url.substr(inx + 1);
  } else {
    paramStr = window.location.search.substr(1);
  }
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = paramStr.match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}
util.showToast = (title, icon = 'none', duration = 2000) => {
  wx.showToast({
    title: title,
    icon: icon,
    duration: duration
  })
}
util.filterImg = function (attachlist) {
 const arr101 = attachlist.filter((item)=>{
  return item.type === '101'
 })
 if(arr101 && arr101.length){
  return arr101;
 } else {
  const arr104 = attachlist.filter((item)=>{
   return item.type === '104'
  })
  if(arr104 && arr104.length){
   return arr104;
  } else {
    return attachlist
  }
 }
}
/*富文本移除HTML标签代码*/
util.removeHTMLTag = function (str) {
  str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
  str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
  str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
  str=str.replace(/ /ig,'');//去掉 
  return str
}
//富文本转意符换成普通字符
util.escape2Html = function (str) { 
 var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}; 
 return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];}); 
}
util.getWeekByDay = function (dayStr, preStr) {
  if (!preStr) {
    preStr = '星期'
  }
  let dayIndex = 0
  if (dayStr) {
    dayIndex = new Date(dayStr).getDay()
  } else {
    dayIndex = new Date().getDay()
  }
  return preStr + weekObj[dayIndex]
}
module.exports = util